package com.LachesismStorm.lachesism.member.controller;

import com.LachesismStorm.lachesism.core.common.response.ResponsePage;
import com.LachesismStorm.lachesism.core.common.response.ResponseResult;
import com.LachesismStorm.lachesism.member.service.YunOManageService;
import com.LachesismStorm.lachesism.member.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lachesism
 * @Date 2024-04-10 16:05
 **/
@RestController
@Api(tags = "通用")
@RequestMapping("/lachesismManage")
public class YunOManageController {
    @Resource
    private YunOManageService yunOManageService;

    @ApiOperation(value = "添加食物分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "foodTypeName", value = "食物种类"),
    })
    @PostMapping("/addFoodTypeName")
    public ResponseResult addFoodTypeName(@RequestParam String foodTypeName) {
        yunOManageService.addFoodTypeName(foodTypeName);
        return ResponseResult.success(null);
    }

    @ApiOperation(value = "获取食物分类列表")
    @PostMapping("/getFoodTypeList")
    public ResponseResult getFoodTypeList() {
        List<SysFoodMenu> sysfoodTyleList = yunOManageService.getFoodTypeList();
        return ResponseResult.success(sysfoodTyleList, null);
    }

    @ApiOperation(value = "添加吃什么")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "foodName", value = "食物名"),
    })
    @PostMapping("/addFood")
    public ResponseResult addFood(@RequestParam String foodName) {
        yunOManageService.addFood(foodName);
        return ResponseResult.success(null);
    }

    @ApiOperation(value = "添加家训")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "StringInfoVo", value = "家训信息"),
    })
    @PostMapping("/addMotto")
    public ResponseResult addMotto(@RequestBody StringInfoVO info) {
        yunOManageService.addMotto(info);
        return ResponseResult.success(null);
    }

    @ApiOperation(value = "访问记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "path", value = "路径"),
    })
    @PostMapping("/accessLog")
    public ResponseResult accessLog(@RequestParam String path) {
        yunOManageService.accessLog(path);
        return ResponseResult.success(null);
    }

    @ApiOperation(value = "获取文件夹下内容")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "FileInfoVO", value = "路径"),
    })
    @PostMapping("/getFolderInfo")
    public ResponseResult getFolderInfo(@RequestBody FileInfoVO info) {
        List<FileInfoVO> fileList = yunOManageService.getFolderInfo(info);
        return ResponseResult.success(fileList, "查询成功");
    }

    @ApiOperation(value = "删除文件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "FileInfoVO", value = "路径"),
    })
    @PostMapping("/deleteFile")
    public ResponseResult deleteFile(@RequestBody FileInfoVO info) {
        Boolean result = yunOManageService.deleteFile(info);
        return ResponseResult.success(result, "删除成功");
    }
    @ApiOperation(value = "查看报错日志")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "第几页"),
    })
    @PostMapping("/getErrorLog")
    public ResponseResult getErrorLog(@RequestParam Integer pageNum) {
        ResponsePage<ExtExceptionLog> extExceptionLogList = yunOManageService.getErrorLog(pageNum);
        return ResponseResult.success(extExceptionLogList, "查询成功");
    }
    @ApiOperation(value = "修改建议")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", name = "SysSuggestions", value = "内容"),
    })
    @PostMapping("/editSuggestion")
    public ResponseResult editSuggestion(@RequestBody SysSuggestions suggestion) {
        yunOManageService.editSuggestion(suggestion);
        return ResponseResult.success(null, "修改成功");
    }
    @ApiOperation(value = "获取文件夹和文件信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "path", value = "路径"),
    })
    @PostMapping("/getFileDetails")
    public ResponseResult getFileDetails(@RequestParam String path) {
        List<FileItem> fileItems = new ArrayList<>();
        File directory = new File(path);

        // 如果目录存在并且是文件夹
        if (directory.exists() && directory.isDirectory()) {
            // 获取目录下的所有文件和文件夹
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 如果是文件夹，添加为文件夹类型
                    if (file.isDirectory()) {
                        fileItems.add(new FileItem(file.getName(), file.getPath(), "folder"));
                    } else { // 如果是文件，添加为文件类型
                        fileItems.add(new FileItem(file.getName(), file.getPath(), "file"));
                    }
                }
            }
        }
        return ResponseResult.success(fileItems, "查询成功");
    }

}
